const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceUser = require("../../../service/service_user.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    proxys: [],
    /**
     * 开始页
     */
    startPage: 0,
    addressShow: false,
    addressData: null
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      ServiceUser.getProxysPage(0, function (data) {
        _this.setData({ proxys: data.records });
      });
    },

    /**
     * 关闭地址
     */
    onCloseAddress: function (e) {
      const _this = this;
      _this.setData({
        addressShow: false,
        addressData: null
      });
    },

    /**
     * 编辑地址
     */
    onEditProxyAddress: function (e) {
      const _this = this;
      const addressData = e.currentTarget.dataset.address;
      //
      _this.setData({
        addressShow: true,
        addressData: addressData
      });
    },

    /**
     * 提交地址
     */
    onSubmitProxyAddress: function (e) {
      const _this = this;
      const addressData = e.detail.value;
      ServiceUser.editProxyAddress(
        { townId: addressData.townId, productId: addressData.productId, username: addressData.username, phone: addressData.phone, address: addressData.address },
        function (data) {
          _this.setData({
            addressShow: false
          });
          _this.onLoad();
        }
      )
    },

    /**
     * 触底事件
     */
    onReachBottom: function () {
      const _this = this;
      let startPage = this.data.startPage + 1;
      ServiceUser.getProxysPage(startPage, function (data) {
        _this.data.proxys.push(...data.records);
        _this.setData({
          proxys: _this.data.proxys,
          startPage: startPage
        });
      });
    }
  }

})